<template>
    <div class="main">
        <div class="main-header">
            <el-button v-if="findButton('添加报价单')" type="primary" @click="addShow = true">添加报价单</el-button>
            <el-button v-if="findButton('编辑报价单')" type="warning" :disabled="'edit' | formatDisabled(selection)"
                @click="editBudget">编辑报价单</el-button>
            <el-button v-if="findButton('删除报价单')" type="danger" :disabled="'delete' | formatDisabled(selection)"
                @click="delBudget">删除报价单</el-button>
            <el-button @click="previewDetail" :disabled="'delete' | formatDisabled(selection)">查看详情</el-button>
        </div>
        <el-table :data="list" border header-cell-class-name="main-table-header" ref="table" class="main-table"
            height="100%" row-key="id" default-expand-all @selection-change="onSelectionChange" @row-click="onRowClick">
            <el-table-column type="selection" width="55"> </el-table-column>
            <el-table-column width="100px" label="报价单ID" prop="id"></el-table-column>
            <el-table-column width="100px" label="所属需求单ID" prop="project_demand_id"></el-table-column>
            <el-table-column width="130px" label="所属需求单项目" prop="project[project_name]"></el-table-column>
            <el-table-column width="130px" label="状态">
                <template slot-scope="{row}">
                    <el-tag :type="row.status==0?'info':row.status==1?'success':row.status==2?'danger':''">
                        {{ row.status == 0 ? '待审核' : row.status == 1 ? '已中标' : row.status == 2 ? '未中标' : '' }}
                    </el-tag>
                </template>
            </el-table-column>
            <!-- <el-table-column width="230px" label="所属项目名称" prop="project[project_name]"></el-table-column> -->
            <el-table-column width="100px" label="预算金额">
                <template slot-scope="{row}">
                    ¥{{ row.budget_money }}
                </template>
            </el-table-column>
            <el-table-column width="100px" label="预算工期">
                <template slot-scope="{row}">
                    {{ row.budget_time }}天
                </template>
            </el-table-column>
            <el-table-column width="120px" label="附件">
                <template slot-scope="{row}">
                    <el-button v-if="row.budget_url" @click.stop="download(row.budget_url)">下载附件</el-button>
                    <span v-else>暂无附件</span>
                </template>
            </el-table-column>
            <el-table-column width="120px" label="会签确认函">
                <template slot-scope="{row}">
                    <el-image @click.stop="$preview(row.checksign)" style="height: 80px;width: 80px;" fit="fit" :src="row.checksign"></el-image>
                </template>
            </el-table-column>
            <el-table-column width="120px" label="项目负责人" prop="project_manager"></el-table-column>
            <el-table-column width="300px" fixed="right" label="操作">
                <template slot-scope="{row}">
                    <el-button v-if="findButton('审核报价单')" :disabled="row.status!=0" type="primary" @click.stop="examine(row.id,1)">同意采纳</el-button>
                    <el-button v-if="findButton('审核报价单')" :disabled="row.status!=0"  type="warning" @click.stop="examine(row.id,2)">拒绝采纳</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination class="main-pagination" :current-page.sync="page" :page-size="limit" :total="total"
            @current-change="onCurrentChange" layout="total,jumper,prev,pager,next"></el-pagination>
        
        <!-- 创建报价单 -->
        <addBudget :preview.sync="preview" v-model="addShow" :projectDemandId.sync="project_demand_id" :editRows.sync="editRows" @close="closeEdit"
            @update="getList"></addBudget>
        <!-- 上传确认函 -->
        <upload :value.sync="examineValue" @update="getList"></upload>
    </div>
</template>
<script>
import addBudget from './add.vue'
import upload from './upload.vue'
export default {
    data() {
        return {
            editRows: [],//编辑的数据
            addShow: false,//添加报价单显示
            examineValue:null,
            project_demand_id: 0,
            list: [],
            selection: [],
            total: 0,
            limit: 20,
            page: 1,
            preview:false,

        }
    },
    components: {
        addBudget,upload
    },
    created() {
        this.getList()
    },
    methods: {
        async getList() {
            let res = await this.$get({
                url: '/admin/project.ProjectBudget/index',
                params: {
                    page: 1,
                    limit: 20
                },
                loading: true
            })
            this.total = res.total
            this.list = res.list
        },
        deleteProjectBudget() {
            this.$confirm(
                `此操作将永久删除该${this.selection.length}个报价单, 是否继续?`,
                "提示",
                {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }
            )
                .then(async () => {
                    let query = "?";
                    for (let i = 0; i < this.selection.length; i++) {
                        const item = this.selection[i];
                        if (i > 0) {
                            query += "&ids[]=" + item.id;
                        } else {
                            query += "ids[]=" + item.id;
                        }
                    }
                    await this.$del({
                        url: "/admin/project.ProjectBudget/del" + query,
                        successTip: true,
                        loading: true
                    });
                    this.getList();
                })
                .catch(() => {
                    this.$refs.table.clearSelection();
                });
        },
        editBudget() {
            this.editRows = this.selection
        },
        delBudget() {
            this.$confirm(
                `此操作将永久删除该${this.selection.length}个报价单, 是否继续?`,
                "提示",
                {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }
            )
                .then(async () => {
                    let query = "?";
                    for (let i = 0; i < this.selection.length; i++) {
                        const item = this.selection[i];
                        if (i > 0) {
                            query += "&ids[]=" + item.id;
                        } else {
                            query += "ids[]=" + item.id;
                        }
                    }
                    await this.$del({
                        url: "/admin/project.ProjectBudget/del" + query,
                        successTip: true,
                        loading: true
                    });
                    this.getList();
                })
                .catch(() => {
                    this.$refs.table.clearSelection();
                });
        },
        // 审核报价单
        async examine(id,status){
            let reason = ''
            if(status==2){
                try{
                    let {value} =  await this.$prompt('请输入不采用理由', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        inputErrorMessage: '请输入不采用理由'
                    })
                    if(!value){
                        this.$message.error('请输入不采用理由')
                        return
                    }
                    reason = value
                }catch(err){
                    return
                }
                await this.$post({
                    url:'/admin/project.ProjectBudget/budgetCheck',
                    data:{
                        status:status,
                        id:id,
                        reason:reason
                    },
                    successTip:true
                })
            }else{
                this.examineValue = {
                    status:status,
                    id:id,
                }
            }
        },
        download(url){
            let a = document.createElement('a')
            a.href = url
            a.download = '附件'
            a.click()
        },
        // 预览
        previewDetail(){
            this.preview = true
            this.editBudget()
        },
        closeEdit() {
            this.editValues = []
        },
        onRowClick(row) {
            this.$refs.table.toggleRowSelection(row);
        },
        onSelectionChange(selection) {
            this.selection = selection;
        },
        onCurrentChange(index) {
            this.page = index;
            this.getList()
        },
    }
}
</script>